#!/bin/bash
REDISCLI="/opt/fonsview/3RD/redis/bin/redis-cli"
PASS="Fonsview@123"
HOST="1.58.190.177"
PORT=6379

if [[ $# == 1 ]];then
    case $1 in
        connected_clients)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS info | grep -w "connected_clients" | awk -F':' '{print $2}'`
            echo $result
        ;;
        blocked_clients)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS info | grep -w "blocked_clients" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory)
            result=`$REDISCLI   -h $HOST -p $PORT -a $PASS info | grep -w "used_memory" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_rss)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS info | grep -w "used_memory_rss" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_peak)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS info | grep -w "used_memory_peak" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_memory_lua)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS info | grep -w "used_memory_lua" | awk -F':' '{print $2}'`
            echo $result
        ;;
        keyspace_hits)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS info | grep -w 'keyspace_hits'| awk -F ':' '{print $2}'`
            echo $result
        ;;
        keyspace_misses)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS info | grep -w 'keyspace_misses'| awk -F ':' '{print $2}'`
            echo $result
        ;;
        used_cpu_sys)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS info | grep -w "used_cpu_sys" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_user)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS info | grep -w "used_cpu_user" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_sys_children)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS info | grep -w "used_cpu_sys_children" | awk -F':' '{print $2}'`
            echo $result
        ;;
        used_cpu_user_children)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS info | grep -w "used_cpu_user_children" | awk -F':' '{print $2}'`
            echo $result
        ;;
        redis_status)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS ping | grep -w 'PONG' | wc -l`
            echo $result
        ;;
        *)
            echo -e "\033[33mUsage: $0 {connected_clients|blocked_clients|used_memory|used_memory_rss|used_memory_peak|used_memory_lua|used_cpu_sys|used_cpu_user|used_cpu_sys_children|used_cpu_user_children|key_hit_rate|redis_status}\033[0m" 
        ;;
    esac
elif [[ $# == 2 ]];then
    case $2 in
        keys)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS info | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $2}'`
            echo $result
        ;;
        expires)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS info | grep -w "$1" | grep -w "keys" | awk -F'=|,' '{print $4}'`
            echo $result
        ;;
        avg_ttl)
            result=`$REDISCLI  -h $HOST -p $PORT -a $PASS info | grep -w "$1" | grep -w "avg_ttl" | awk -F'=|,' '{print $6}'`
            echo $result
        ;;
        *)
            echo -e "\033[33mUsage: $0 {db0 keys|db0 expires|db0 avg_ttl}\033[0m" 
        ;;
    esac
fi
